<meta charset="utf-8">
(#) Invalid string escapes

!!! ERROR: Invalid string escapes
   This is an error.

Id
:   `StringEscaping`
Summary
:   Invalid string escapes
Severity
:   Error
Category
:   Correctness: Messages
Platform
:   Android
Vendor
:   Android Open Source Project
Feedback
:   https://issuetracker.google.com/issues/new?component=192708
Since
:   7.3.0 (September 2022)
Affects
:   Resource files
Editing
:   This check runs on the fly in the IDE editor
Implementation
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/StringEscapeDetector.kt)
Tests
:   [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/StringEscapeDetectorTest.kt)

Apostrophes (') must always be escaped (with a \\\\), unless they appear
in a string which is itself escaped in double quotes (\").

!!! Tip
   This lint check has an associated quickfix available in the IDE.

(##) Example

Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
res/values/strings.xml:3:Error: Apostrophe not preceded by \
[StringEscaping]
&lt;string name="some_string"&gt;'ERROR'&lt;/string&gt;
                           ^
res/values/strings.xml:5:Error: Apostrophe not preceded by \
[StringEscaping]
&lt;string name="some_string3"&gt;What's New&lt;/string&gt;
                                ^
res/values/strings.xml:12:Error: Bad character in \u unicode escape
sequence [StringEscaping]
&lt;string name="some_string10"&gt;Unicode\u12.&lt;/string&gt;
                                        ^
res/values/strings.xml:19:Error: Apostrophe not preceded by \
[StringEscaping]
  &lt;item&gt;It's incorrect&lt;/item&gt;
          ^
res/values/strings.xml:23:Error: Apostrophe not preceded by \
[StringEscaping]
    &lt;item quantity="few"&gt;%d piose'nki.&lt;/item&gt;
                                 ^
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here is the source file referenced above:

`res/values/strings.xml`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~xml linenumbers
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
&lt;string name="some_string"&gt;'ERROR'&lt;/string&gt;
&lt;string name="some_string2"&gt;"'OK'"&lt;/string&gt;
&lt;string name="some_string3"&gt;What's New&lt;/string&gt;
&lt;string name="some_string4"&gt;Unfinished\&lt;/string&gt;
&lt;string name="some_string5"&gt;Unicode\u&lt;/string&gt;
&lt;string name="some_string6"&gt;Unicode\u1&lt;/string&gt;
&lt;string name="some_string7"&gt;Unicode\u12&lt;/string&gt;
&lt;string name="some_string8"&gt;Unicode\u123&lt;/string&gt;
&lt;string name="some_string9"&gt;Unicode\u1234&lt;/string&gt;
&lt;string name="some_string10"&gt;Unicode\u12.&lt;/string&gt;
&lt;string name="news"&gt;  "  What's New "    &lt;/string&gt;
&lt;string name="space_slash"&gt; \&lt;/string&gt;
&lt;string name="space_slash2"&gt;  \&lt;/string&gt;
&lt;string name="space_slash3"&gt;   \&lt;/string&gt;
&lt;string-array name="array_of_string"&gt;
  &lt;item&gt;It\'s correct&lt;/item&gt;
  &lt;item&gt;It's incorrect&lt;/item&gt;
&lt;/string-array&gt;
&lt;plurals name="numberOfSongsAvailable"&gt;
    &lt;item quantity="one"&gt;%d piosenkę.&lt;/item&gt;
    &lt;item quantity="few"&gt;%d piose'nki.&lt;/item&gt;
    &lt;item quantity="other"&gt;%d piosenek.&lt;/item&gt;
&lt;/plurals&gt;
&lt;string name="foo"&gt;Letzte Freischaltung:\\ &lt;/string&gt; &lt;!-- b/387281249 --&gt;
&lt;/resources&gt;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can also visit the
[source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/StringEscapeDetectorTest.kt)
for the unit tests for this check to see additional scenarios.

(##) Suppressing

You can suppress false positives using one of the following mechanisms:

* Adding the suppression attribute `tools:ignore="StringEscaping"` on
  the problematic XML element (or one of its enclosing elements). You
  may also need to add the following namespace declaration on the root
  element in the XML file if it's not already there:
  `xmlns:tools="http://schemas.android.com/tools"`.

  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;resources xmlns:tools="http://schemas.android.com/tools"&gt;
      ...
      &lt;string tools:ignore="StringEscaping" .../&gt;
    ...
  &lt;/resources&gt;
  ```

* Using a special `lint.xml` file in the source tree which turns off
  the check in that folder and any sub folder. A simple file might look
  like this:
  ```xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;lint&gt;
      &lt;issue id="StringEscaping" severity="ignore" /&gt;
  &lt;/lint&gt;
  ```
  Instead of `ignore` you can also change the severity here, for
  example from `error` to `warning`. You can find additional
  documentation on how to filter issues by path, regular expression and
  so on
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html).

* In Gradle projects, using the DSL syntax to configure lint. For
  example, you can use something like
  ```gradle
  lintOptions {
      disable 'StringEscaping'
  }
  ```
  In Android projects this should be nested inside an `android { }`
  block.

* For manual invocations of `lint`, using the `--ignore` flag:
  ```
  $ lint --ignore StringEscaping ...`
  ```

* Last, but not least, using baselines, as discussed
  [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).

<!-- Markdeep: --><style class="fallback">body{visibility:hidden;white-space:pre;font-family:monospace}</style><script src="markdeep.min.js" charset="utf-8"></script><script src="https://morgan3d.github.io/markdeep/latest/markdeep.min.js" charset="utf-8"></script><script>window.alreadyProcessedMarkdeep||(document.body.style.visibility="visible")</script>